package com.bsoft.hospitalchs.ui.childrecord;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bsoft.hospitalchs.R;
import com.bsoft.hospitalchs.base.BaseFragment;
import com.bsoft.hospitalchs.model.InspectBean;
import com.bsoft.hospitalchs.model.RecordInfo;
import com.bsoft.hospitalchs.model.RecordResBean;
import com.bsoft.hospitalchs.model.SearchResBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.socks.library.KLog;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by 泅渡者
 * Created on 2017/5/18.
 * 儿童预检记录
 */

public class ChildrecordFragment extends BaseFragment<ChildrecordContract.Presenter> implements ChildrecordContract.View {


    @Bind(R.id.et_temperature)
    EditText etTemperature;
    @Bind(R.id.et_heart)
    EditText etHeart;
    @Bind(R.id.textView6)
    TextView textView6;
    @Bind(R.id.et_pulse)
    EditText etPulse;
    @Bind(R.id.et_weight)
    EditText etWeight;
    @Bind(R.id.et_height)
    EditText etHeight;
    @Bind(R.id.et_blood_low)
    EditText etBloodLow;
    @Bind(R.id.et_blood_hight)
    EditText etBloodHight;
    @Bind(R.id.btn_save)
    Button btnSave;
    @Bind(R.id.rg_gp)
    RadioGroup rg_gp;
    @Bind(R.id.rb_01)
    RadioButton rb_01;
    @Bind(R.id.rb_02)
    RadioButton rb_02;
    @Bind(R.id.edt_desc)
    EditText edt_desc;

    SearchResBean searchResBean;
    @Override
    public int onSetLayoutId() {
        return R.layout.fragment_record;
    }

    @Override
    public void initView() {
        this.createPresenter(new ChildrecordPresenter(this, getActivity()));

        Gson gson = new Gson();
        searchResBean = gson.fromJson(getActivity().getIntent().getStringExtra("SearchResBean").toString(), new TypeToken<SearchResBean>() {
        }.getType());
        RecordInfo recordInfo = new RecordInfo();
        recordInfo.setServiceId("phis.mobileService");
        recordInfo.setMethod("execute");
        recordInfo.setServiceAction("getRegDataPad");
        RecordInfo.BodyBean bodyBean = new RecordInfo.BodyBean();
        bodyBean.setEMPIID(searchResBean.getBody().getEMPIID());
        recordInfo.setBody(bodyBean);
        presenter.getRecord(recordInfo);


    }


    @Override
    public void hideLoading() {

    }

    @Override
    public void showLoadingDialog() {

    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
        InspectBean inspectBean = new InspectBean();
        inspectBean.setMethod("execute");
        inspectBean.setServiceAction("saveRegData");
        inspectBean.setServiceId("phis.mobileService");
        InspectBean.BodyBean bodyBean = new InspectBean.BodyBean();
        bodyBean.setEMPIID(searchResBean.getBody().getEMPIID());
        bodyBean.setBODYTEMPERATURE(getEdtStr(etTemperature));
        bodyBean.setDBP(getEdtStr(etBloodLow));
        bodyBean.setHEARTRATE(getEdtStr(etHeart));
        bodyBean.setPULSERATE(getEdtStr(etPulse));
        bodyBean.setSBP(getEdtStr(etBloodHight));
        bodyBean.setWEIGHT(getEdtStr(etWeight));
        bodyBean.setHEIGHT(getEdtStr(etHeight));
        if(rg_gp.getCheckedRadioButtonId()==rb_01.getId()){
            bodyBean.setPREFLIGHTTYPE("0");
        }else if(rg_gp.getCheckedRadioButtonId()==rb_02.getId()){
            bodyBean.setPREFLIGHTTYPE("1");
        }
        bodyBean.setREMARK(getEdtStr(edt_desc));
        inspectBean.setBody(bodyBean);
        KLog.d(inspectBean.toString());
        presenter.toSaveinfo(inspectBean);
    }

    private String getEdtStr(EditText editText){
        if(TextUtils.isEmpty(editText.getText().toString())){
            return "";
        }else{
            return editText.getText().toString();
        }
    }

    @Override
    public void showRecord(RecordResBean recordResBean) {
        etTemperature.setText(recordResBean.getBody().getBODYTEMPERATURE());
        etHeart.setText(recordResBean.getBody().getHEARTRATE());
        etPulse.setText(recordResBean.getBody().getPULSERATE());
        etWeight.setText(recordResBean.getBody().getWEIGHT());
        etHeight.setText(recordResBean.getBody().getHEIGHT());
        etBloodLow.setText(recordResBean.getBody().getDBP());
        etBloodHight.setText(recordResBean.getBody().getSBP());
        edt_desc.setText(recordResBean.getBody().getREMARK());
        if(recordResBean.getBody().getPREFLIGHTTYPE().equals("0")){
            rb_01.setChecked(true);
        }else if(recordResBean.getBody().getPREFLIGHTTYPE().equals("1")){
            rb_02.setChecked(true);
        }
    }
}
